#include "mytools.h"

myTools::myTools()
{

}


void myTools::Byte2Ascii(QByteArray des, QByteArray src, quint16 cnt)
{
    qint8 high;
    qint8 low;
    quint16 i = cnt;
    for (i = 0; i < cnt; i++)
    {
        high = (src[i] >> 4);
        low = (src[i] & 0xf);
        if (high > 9)
            high = (high + 'A' - 0xA);
        else
            high = (high + '0');
        des[i * 2] = high;

        if (low > 9)
            low = (low + 'A' - 0xA);
        else
            low = (low + '0');
        des[i * 2 + 1] = low;
    }
}

quint32 myTools::Ascii2Byte(QByteArray des, QByteArray src, quint16 cnt)
{
    qint32 i = 0;
    qint32 j = 0;
    qint8 highData;
    qint8 lowData;
    while (i < cnt)
    {
        if (src[i] == ' ')
        {
            i++;
            continue;
        }
        if (i >= src.length())
        {
            //MessageBox.Show("数据长度错误");
            return 0;
        }
        if ((0x0D == src[i]) && (0x0A == src[i + 1]))
        {
            return j;
        }
        if (src[i] <= '9' && src[i] >= '0')
            highData = (src[i] - '0');
        else
        {
            if ((src[i] >= 'A') && (src[i] <= 'Z'))
            {
                highData = (src[i] - 'A' + 0xa);
            }
            else if ((src[i] >= 'a') && (src[i] <= 'z'))
            {
                highData = (src[i] - 'a' + 0xa);
            }
            else
            {
                //MessageBox.Show ("输入的数据无法识别");
                return 0;
            }
        }
        i++;
        if (i >= src.length())
        {
            //MessageBox.Show("数据长度错误");
            return 0;
        }

        if (src[i] <= '9' && src[i] >= '0')
            lowData = (src[i] - '0');
        else
        {
            if ((src[i] >= 'A') && (src[i] <= 'Z'))
            {
                lowData = (src[i] - 'A' + 0xa);
            }
            else if ((src[i] >= 'a') && (src[i] <= 'z'))
            {
                lowData = (src[i] - 'a' + 0xa);
            }
            else
            {
                //MessageBox.Show ("数据长度错误");
                return 0;
            }
        }

        des[j] = (lowData + (highData << 4));
        j++;
        i++;
    }
    return j;
}
